﻿using FluentValidation.Results;

using System;
using System.Collections.Generic;
using System.Linq;

namespace Holla.Infrastructure.Mediator
{
    internal class ValidationFailedException : Exception
    {
        public IDictionary<string, string[]> Failures { get; }

        public ValidationFailedException() : base("One or more validation failed.")
        {
            Failures = new Dictionary<string, string[]>();
        }

        public ValidationFailedException(List<ValidationFailure> failures) : this()
        {
            foreach (var propertyName in failures.Select(p => p.PropertyName).Distinct())
            {
                var propertyFailures = failures
                    .Where(p => p.PropertyName == propertyName)
                    .Select(p => p.ErrorMessage)
                    .ToArray();
                Failures.Add(propertyName, propertyFailures);
            }
        }
    }
}